<?php
/**
 * Created by PhpStorm.
 * User: 王杰 wj@ourstu.com
 * Date: 2017/1/4
 * Time: 9:10
 */
namespace Mall\Controller;

use Think\Controller;

class IndexController extends Controller
{

    public function _initialize()
    {
        $this->setTitle("商城");
        $this->setKeywords("商城");
        $this->setDescription("商城");
        $this->assign('bottom_flag','find');
    }

    /**
     * 主页面显示
     */
    public function index()
    {
        $aPage = I('get.page',1,'intval');
        $aCount = I('get.count',10,'intval');
        $aIsPull = I('get.is_pull',0,'intval');
        $aType = I('get.type','','intval');

        $cateList = D('Mall/GoodsCategory')->getCategory();
        $this->assign('cate',$cateList);

        $goodsModel = D('Goods');
        if (!empty($aType)) {
            $temp = array_column($cateList,'id');
            if (!in_array($aType,$temp)) {
                $this->error('参数错误~');
            }
            $this->assign('type',$aType);

            $cateGoods = array();
            $cateIds = $goodsModel->getCateGoods($aType);
            $cateId = array_column($cateIds,'id');
            foreach ($cateId as $key => $v) {
                $cateGoods[] = $goodsModel->getGoods($v);
                $cateGoods[$key]['pictures'] = explode(',', $cateGoods[$key]['pictures']);
                $cateGoods[$key]['img'] = getThumbImageById($cateGoods[$key]['pictures'][0],160,160);
            }
            $this->assign('cate_goods',$cateGoods);
        }
        $good = $goodsModel->getAllGoodsByPage($aPage,$aCount);
        $ids = array_column($good, 'id');
        $goods = array();
        foreach ($ids as $key => $v) {
            $goods[] = $goodsModel->getGoods($v);
            $goods[$key]['pictures'] = explode(',', $goods[$key]['pictures']);
            $goods[$key]['img'] = getThumbImageById($goods[$key]['pictures'][0],160,160);
        }
        $this->assign('goods',$goods);

        $new_time = modC('MALL_NEW_LEFT',3);
        $time_left = get_some_day($new_time);
        $param['update_time'] = array('gt', $time_left);
        $param['status'] = 1;
        $newGoods = $goodsModel->order('update_time desc')->where($param)->limit(10)->select();
        foreach ($newGoods as &$v) {
            $v['pictures'] = explode(',',$v['pictures']);
            $v['img'] = getThumbImageById($v['pictures'][0],160,160);
        }
        unset($v);
        $this->assign('new_goods',$newGoods);

        $hotGoods = array();
        $hotIds = $goodsModel->getHotGoods();
        $hotId = array_column($hotIds, 'id');
        foreach ($hotId as $key => $v) {
            $hotGoods[] = $goodsModel->getGoods($v);
            $hotGoods[$key]['pictures'] = explode(',', $hotGoods[$key]['pictures']);
            $hotGoods[$key]['img'] = getThumbImageById($hotGoods[$key]['pictures'][0],160,160);
        }
        $this->assign('hot_goods',$hotGoods);

        $this->assign('mall_pictures',getThumbImageById(modC('MALL_PICTURES','','Mall'),1080,720));
        $this->assign('mall_name',modC('MALL_NAME','智果内容付费系统','Mall'));
        $this->assign('mall_intro',modC('MALL_INTRO','未填写社区简介~','Mall'));
        $this->assign('first_mall_num', count($goods));
        if ($aIsPull) {
            $data['html'] = '';
            $data['status'] = 1;
            $data['html'] .= $this->fetch('_list');
            $this->ajaxReturn($data);
        }
        $this->display();
    }
    
    public function detail(){
        $aId = I('get.id',0,'intval');
        if (empty($aId)) {
            $this->error('此商品不存在');
        }
        $good = D('Goods')->getGoods($aId);
        if (empty($good)) {
            $this->error('此商品不存在');
        }
        S('goods_' . $aId, null);
        M('mall_goods')->where(array('id'=>$aId))->setInc('views',1);
        $this->setTitle($good['name']);
        $this->setKeywords($good['name']);
        $this->setDescription($good['name']);
        $collect['module'] = 'Mall';
        $collect['table'] = 'collect';
        $collect['row'] = $aId;
        $collect['uid']=is_login();
        $isCollect = D('Collect')->isCollect($collect);
        if ($isCollect) {
            $good['is_collect'] = 1;
        } else {
            $good['is_collect'] = 0;
        }
        $good['pictures'] = explode(',',$good['pictures']);
        $uid=is_login();
        $is_pay=D('Order/OrderGoods')->isPayed($uid,$aId);
        $good['content'] = M('mall_goods_article')->where(array('goods_id'=>$aId))->getField('content');
        $this->assign('good',$good);
        $this->assign('is_pay',$is_pay);
        $this->assign('is_login',$uid);
        $this->display();
    }

    public function getGoods($type='common')
    {
        $uid = is_login();
        if (!$uid) {
            $this->ajaxReturn('error');
        }
        $goodsModel = D('Goods');
        if ($type == 'hot') {
            $good = $goodsModel->getHotGoods();
        } else {
            $good = $goodsModel->getAllGoods();
        }
        $ids = array_column($good, 'id');
        $goods = array();
        foreach ($ids as $key => $v) {
            $goods[] = $goodsModel->getGoods($v);
            $goods[$key]['img'] = getThumbImageById($goods[$key]['pictures'],160,160);
        }
        $this->ajaxReturn($goods);
    }

    public function searchGoods()
    {
        $aPage = I('get.page', 1, 'intval');
        $aCount = I('get.count',10,'intval');
        $aGoodsName = I('get.keywords', '', 'text');
        $goods = D('Mall/Goods')->where(array('name'=>array('like','%'.$aGoodsName.'%')))->page($aPage,$aCount)->select();
        foreach ($goods as &$v) {
            $v['pictures'] = explode(',', $v['pictures']);
            $v['img'] = getThumbImageById($v['pictures'][0],160,160);
        }
        unset($v);
        $this->ajaxReturn($goods);
    }
    public function bought()
    {
        $map['uid']=is_login();
        $map['is_pay']=array('eq',1);
        $book_id=D('order_goods')->order('pay_time desc')->where($map)->select();
        $i=0;
        foreach ($book_id as $vo){
            $book_message[$i]=D('mall_goods')->where(array('id'=>$vo['goods_id']))->find();
            $i++;
        }
        unset($vo);
        foreach ($book_id as $key => $v) {
        $book_message[$key]['pictures'] = explode(',', $book_message[$key]['pictures']);
        $book_message[$key]['img'] = getThumbImageById($book_message[$key]['pictures'][0],160,200);
        }
        unset($key);
        unset($v);
        $this->assign('book_list',$book_message);
        $this->display();
    }
    public function article()
    {
        $aId = I('get.id',0,'intval');
        $good = D('Goods')->getGoods($aId);
        S('goods_' . $aId, null);
        $this->setTitle($good['name']);
        $this->setKeywords($good['name']);
        $this->setDescription($good['name']);
        $good['content'] = M('mall_goods_article')->where(array('goods_id'=>$aId))->getField('content');
        $this->assign('good',$good);
        $this->display();
    }
    //热门搜索
    public function search(){
        $change=D("mall_goods");  //换一批之后，搜索页面显示的热门搜索内容
        $parme=array('status'=>'1');
        if(IS_POST){
            $page=I("post.page","","intval");
            $result=$change->where($parme)->order("views desc")->page($page,8)->getfield("name",true);
            $myId=$change->where($parme)->order("views desc")->page($page,8)->getfield("id",true);
            if($result==null){
                $result=$change->where($parme)->order("views desc")->page(1,8)->getfield("name",true);
                $myId=$change->where($parme)->order("views desc")->page(1,8)->getfield("id",true);
                $res['is']=0;
            }

            $res['status']="1";
            $res['data']=$result;
            $res['id']=$myId;
            $this->ajaxReturn($res);
        }
        else{
            $history=D("mall_search"); //点击搜索之后，进入到搜索页面显示的热门搜索内容
            $result=$change->where($parme)->order("views desc")->page(1,8)->select();
            $this->assign("result",$result);
            $historyResult=$history->where(array("uid"=>get_uid()))->order("create_time desc")->limit(3)->select();
            $this->assign("historyResult",$historyResult);
            $this->display();
        }
    }

    public function allSearch(){
        $is=0;
        $title=I("post.title","","text");
        $parame['status']=1;
        $parame['name']=array("like","%".$title."%");
        $change=D("mall_goods");
        if(I("post.is","","text")=="no"){
            $result=$change->where($parame)->select();
            if(!$result){
                $this->ajaxReturn(array("status"=>"1","html"=>"none"));
            }
            foreach ($result as &$val) {
                $val['img']=getThumbImageById($val['pictures'],160,160);
            }
            unset($val);
            $this->assign("goods", $result);
            $html = $this->fetch('_list');
            $this->ajaxReturn(array("status" => "1","html" => $html));
        }
        $result=$change->where($parame)->select();
        $history=D("mall_search");
        $historyContent=$history->where(array('uid'=>get_uid()))->select();
        foreach ($historyContent as $val){
            if($val["historical"]==$title){
                $is=1;
            } ;
        }
        if($is==0){
            $goods["uid"]=get_uid();
            $goods["historical"]=$title;
            $goods["create_time"]=time();
            $history->add($goods);
        }

        $historyResult=$history->where(array("uid"=>get_uid()))->order("create_time desc")->limit(3)->select();

        $this->assign("historyResult",$historyResult);
        $myHtml=$this->fetch("_history");
        if(!$result){
            $this->ajaxReturn(array("status"=>"1","html"=>"none","goods"=>$myHtml));
        } else {
            foreach ($result as &$val) {
                $val['img']=getThumbImageById($val['pictures'],160,160);
            }
            unset($val);
            $this->assign("goods", $result);
            $html = $this->fetch('_list');
            $this->ajaxReturn(array("status" => "1", "html" => $html,"goods"=>$myHtml));
        }
    }

    /**
     * delete
     * 删除一条历史记录
     * @auth wb
     */
    public function delete(){
        $history=D("mall_search");
        $id=I("post.id","","intval");
        $history->where(array("uid"=>get_uid(),"id"=>$id))->delete();
        $historyResult=$history->where(array("uid"=>get_uid()))->order("create_time desc")->limit(3)->select();
        $this->assign("historyResult",$historyResult);
        $myHtml=$this->fetch("_history");
        $this->ajaxReturn(array("status" => "1", "html" => $myHtml));
    }

    /**
     * allDelete
     * 清空历史记录
     * @auth wb
     */
    public function allDelete(){
        $history=D("mall_search");
        $history->where(array("uid"=>get_uid()))->delete();
        $this->ajaxReturn(array("status" => "1"));
    }
}